/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mail;

import java.util.Date;
import java.util.Properties;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Alexandre
 */
@ManagedBean
@RequestScoped
public class EnviarEmailMB {

    public void enviarEmail() {
        Properties p = new Properties();
        p.put("mail.host", "smtp.live.com");
        p.put("mail.smtp.port", "25");
        Session session = Session.getInstance(p, null);
        MimeMessage msg = new MimeMessage(session);
        try {
            // "de" e "para"!!  
            msg.setFrom(new InternetAddress("alexandreheinen@hotmail.com"));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress("heinen_ale@hotmail.com"));

            // nao esqueca da data!  
            // ou ira 31/12/1969 !!!  
            msg.setSentDate(new Date());

            msg.setSubject("assunto da mensagem");

            msg.setText("corpo da mensagem");

            // evniando mensagem (tentando)  
            Transport.send(msg);
        } catch (AddressException e) {
            System.out.println("Catch AddressException");
        } catch (MessagingException e) {
            System.out.println(e.toString());
        }
    }
}
